import {reqNotice, reqCreateNotice, reqDeleteNoticeByIds, reqGetNoticesByCondition, reqUpdateNotice, reqGetNoticeList } from "@/api";
import { Message } from "element-ui";
export default({
    namespaced:true,
    actions:{
        async getNotices(context){
            let result= await reqGetNoticeList();
            if(result.code==='200'){
             context.commit('getNotices',result.other);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
         },
        async createNotice(context,value){
            let result=await reqCreateNotice(value);
            if(result.code==='200'){
                context.commit('createNotice',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateNotice(context,value){
            let result=await reqUpdateNotice(value);
            if(result.code==='200'){
                context.commit('updateNotice',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteNotices(context,value){
            let result=await reqDeleteNoticeByIds(value);
            Message({
                type: 'success',
                message: '正在删除...'
            })
            if(result.code==='200'){
                context.commit('deleteNotice',value)
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async getNoticeByCondition(context,value){
           let result=await reqGetNoticesByCondition(value);
           if(result.code==='200'){
            context.commit('getNoticeByCondition',result.other)
           }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        }
    },
    mutations:{
        getNotices(state,value){
            state.notices=value;
        },
        createNotice(state,value){
            let obj=value;
            state.notices.unshift(obj);
        },
        updateNotice(state,value){
            let obj=value;
            state.notices.forEach((element,index)=>{
                if(obj.id===element.id){
                    state.notices[index]=obj;
                }
            })
        },
        deleteNotice(state,value){
            let delArr=value;
            let originArr=state.notices;
            delArr.forEach((element,index) => {
                originArr.forEach((e,i)=>{
                    if(element===e.id){
                        originArr.splice(i,1)
                    }
                })
            });
        },
        getNoticeByCondition(state,value){
            state.notices=value;
        }
    },
    state:{
        notices:[]
    },
    getters:{

    }
})